<?php
/**
 * Created by 项目.
 * User: xuguopeng
 * Date: 6/10/21
 * Time: 4:24 AM
 */

namespace app\api\controller\v1;


use app\api\model\Product;
use app\api\model\ProductOrderContent;
use app\api\service\Token as TokenService;

class TeamOwnerRank
{
    // 产品销量
    public function getTeamOwnerRankProduct()
    {
        TokenService::getCurrentUid();
        $product = Product::with('store')->select()->toArray();
        $array = [];
        foreach ($product as $k=>$v){
            $product[$k]['count'] = ProductOrderContent::where('product_id','eq',$v['id'])
                ->where('status','gt',1)->sum('product_number');
            if($product[$k]['count'] > 0){
                array_push($array,$product[$k]);
            }
        }
        if(count($array) > 0) {
            $array = $this->arraySort($array,'count');
        }
        return $array;
    }

    /**
     * 二维数组根据某个字段排序
     * @param array $array 要排序的数组
     * @param string $keys   要排序的键字段
     * @param string $sort  排序类型  SORT_ASC     SORT_DESC
     * @return array 排序后的数组
     */
    public function arraySort($array, $keys, $sort = SORT_DESC) {
        $keysValue = [];
        foreach ($array as $k => $v) {
            $keysValue[$k] = $v[$keys];
        }
        array_multisort($keysValue, $sort, $array);
        return $array;
    }
}